package net.noday.service.impl;

import java.util.List;

import net.noday.domain.Article;
import net.noday.persistent.jdbcImpl.JdbcArticleDAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl {

	@Autowired
	private JdbcArticleDAO articleDAO;
	
	public boolean add(Article article) {
		boolean status = false;
		try {
			article = articleDAO.save(article);
			status = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return status;
	}
	
	public Article loadById(int id) {
		return articleDAO.loadById(id);
	}
	
	public void update(Article article) {
		articleDAO.update(article);
	}
	
	public void delete(int id) {
		articleDAO.delById(id);
	}
	
	public List<Article> getAll() {
		return articleDAO.listAll();
	}
	
	/**
	 * 分页 升序
	 * @param pageFrom 当前页
	 * @param pageTo 目标页
	 * @param uid 最小或最大id
	 * @param pageSize 每页数量
	 * @return
	 */
	public List<Article> getPageByASC(int pageFrom, int pageTo, long id, int pageSize) {
		int value = pageTo - pageFrom;
		boolean forword = value > 0;
		return articleDAO.listPageByASC(new Object[]{forword ? ">" : "<", id, 
				forword ? "asc" : "desc", (value - 1) * pageSize, pageSize});
	}
	public List<Article> getPageByDESC(int pageFrom, int pageTo, long id, int pageSize) {
		int value = pageTo - pageFrom;
		boolean forword = value > 0;
		return articleDAO.listPageByDESC(new Object[]{forword ? "<" : ">", id, 
				forword ? "desc" : "asc", (value - 1) * pageSize, pageSize});
	}
}
